﻿#region Using

using Common.Base;
using Common.Enums;

using GameCommon.ManagerInterfaces;
using GameCommon.Services;

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

#endregion

namespace GameCommon.Manager
{
    internal class ContentManager : IContentManager
    {
        private readonly Microsoft.Xna.Framework.Content.ContentManager content;

        private readonly GraphicsDevice graphicsDevice;

        public ContentManager(Microsoft.Xna.Framework.Content.ContentManager content, GraphicsDevice graphicsDevice)
        {
            this.content = content;
            this.graphicsDevice = graphicsDevice;
        }

        public Texture2D GetBoardTexture2D(string name, int zoom)
        {
            if (!string.IsNullOrEmpty(name))
            {
                return this.GetTexture2D(Service.PathService.GetBoardTexture2D(name, zoom));
            }

            return null;
        }

        public Texture2D GetCloudTexture2D(string name)
        {
            if (!string.IsNullOrEmpty(name))
            {
                return this.GetTexture2D(Service.PathService.GetCloudTexture2D(name));
            }

            return null;
        }

        public Texture2D GetBoardTexture2D(string name)
        {
            if (!string.IsNullOrEmpty(name))
            {
                return this.GetTexture2D(Service.PathService.GetBoardTexture2D(name));
            }

            return null;
        }

        public Texture2D GetFontTexture2D(FontEnum font, FontSizeEnum size)
        {
            return this.GetTexture2D(Service.PathService.GetFontTexture(font, size));
        }

        public Effect GetEffect(string name)
        {
            return this.content.Load<Effect>(Service.PathService.GetEffect(name));
        }

        public int GetViewportHeight()
        {
            return this.graphicsDevice.Viewport.Height;
        }

        public Dimension GetViewportSize()
        {
            return new Dimension(0, 0, this.graphicsDevice.Viewport.Width, this.graphicsDevice.Viewport.Height);
        }

        public int GetViewportWidth()
        {
            return this.graphicsDevice.Viewport.Width;
        }

        public Texture2D GetMenuTexture2D(string name)
        {
            if (!string.IsNullOrEmpty(name))
            {
                return this.GetTexture2D(Service.PathService.GetMenuTexture2D(name));
            }

            return null;
        }

        public Texture2D GetMiscTexture2D(string name)
        {
            if (!string.IsNullOrEmpty(name))
            {
                return this.GetTexture2D(Service.PathService.GetMiscTexture2D(name));
            }

            return null;
        }

        public Texture2D GetHudTexture2D(string name)
        {
            if (!string.IsNullOrEmpty(name))
            {
                return this.GetTexture2D(Service.PathService.GetHudTexture2D(name));
            }

            return null;
        }

        public Texture2D GetRectangle(Color color)
        {
            var texture = new Texture2D(this.graphicsDevice, 1, 1, false, SurfaceFormat.Color);
            texture.SetData(new[] { color });
            return texture;
        }

        /// <summary>
        /// Creates a Rendertarget with the Size of the Viewport.
        /// </summary>
        /// <returns>Rendertarged which can be used like a texture</returns>
        public RenderTarget2D GetRenderTarget2D()
        {
            return new RenderTarget2D(this.graphicsDevice, this.graphicsDevice.Viewport.Width, this.graphicsDevice.Viewport.Height);
        }

        public void SetRenderTarget2D(RenderTarget2D target)
        {
            this.graphicsDevice.SetRenderTarget(target);
        }

        public void ClearGraphics()
        {
            this.graphicsDevice.Clear(Color.Transparent);
        }

        /// <summary>
        /// Loads the texture into RAM
        /// </summary>
        /// <param name="path">Path to Texture</param>
        public void LoadTexture(string path)
        {
            this.GetTexture2D(path);
        }

        private Texture2D GetTexture2D(string path)
        {
            return this.content.Load<Texture2D>(path);
        }
    }
}